package cn.hchaojie.camp.day11;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.junit.Test;

// 源代码的编码会影响我们字符流的默认编码

// 不管源代码是什么编码，我们都可以通过手动指定输入流的编码来正确的读取文件
public class DemoReader {
	@Test
	public void test() {
		String path = "C:\\Users\\hchaojie\\Pictures\\video\\倚天屠龙记utf-8.txt";
		try (

			InputStreamReader is = new InputStreamReader(new FileInputStream(path), "utf-8");
				) {
			
			char[] chars = new char[1024];
			
			int count = 0;
			while ((count = is.read(chars)) != -1) {
				System.out.println(new String(chars, 0, count));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test2() {
		String path = "C:\\Users\\hchaojie\\Pictures\\video\\倚天屠龙记gbk.txt";
		try (FileReader reader = new FileReader(path);) {
			char[] chars = new char[1024];
			
			int count = 0;
			while ((count = reader.read(chars)) != -1) {
				System.out.println(new String(chars, 0, count));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 缓冲流，专门用来读大文件
	@Test
	public void test3() {
//		// 字节输入流
//		InputStream is = new FileInputStream("a.txt");
//		// 字符输入流
//		InputStreamReader reader = new InputStreamReader(is, "gbk");
//		
//		// 缓冲流
//		BufferedReader br = new BufferedReader(reader);
		
		
		String path = "C:\\Users\\hchaojie\\Pictures\\video\\倚天屠龙记gbk.txt";
		try (
			BufferedReader br = new BufferedReader(
					new InputStreamReader(
							new FileInputStream(path), "gbk"));
				) {
			
			String line = null;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}